<?php

class Story_Widget_CreateStoryThreadController extends
    Engine_Content_Widget_Abstract
{
    public function indexAction()
    {
    	$viewer = Engine_Api::_()->user()->getViewer();
    	$this->view->can_create = Engine_Api::_()->authorization()->isAllowed('story', $viewer, 'create');
    	
    	if (!$this->view->can_create) {
    		return $this->setNoRender();
    	}
    	$story = null;    	
    	$chapter_number = 1;
    	$prevChapter = null;
		$this->view->is_owner = true;
    	if( Engine_Api::_()->core()->hasSubject() ) {
    		$story = Engine_Api::_()->core()->getSubject();
    		
    		if(!$story->canView(Story_Model_Category::STORY_THREAD)){    			
    			return $this->setNoRender();
    		}   
    		$story_id = $story->getIdentity();    		
    		
    		$request = Zend_Controller_Front::getInstance()->getRequest();
    		$type = $request->getParam('type');
    		
    		$request = Zend_Controller_Front::getInstance()->getRequest();
    		$chapter_id = $request->getParam('chapter_id');
    		$prevChapter = $story->getLastChapterThread();

    		if(empty($type)){
    			$type = "edit";
                
                if($story->owner_id==$viewer->getIdentity()&&$story->invite_request == 2){               
                    return $this->setNoRender();
                }  
                if($story->owner_id!=$viewer->getIdentity()&&$story->invite_request == 1){               
                    return $this->setNoRender();
                }       
    		}
    		
    		if($type == "invite"){
                // Convert STORY_THREAD to NOVEL
                if($story->category_id==Story_Model_Category::STORY_THREAD)
                    $story->category_id = Story_Model_Category::NOVEL;
                $story->save();
                
    			$friends = $viewer->membership()->getMembers();
    			$this->view->friends = $friends;
    			$this->view->friend_count = count($friends); 
    			$this->view->form = $form = new Story_Form_Create_Invite(array(
    					'story' => $story->getIdentity(),    					
    			));

                $default_value = false;
    			if(count($friends) > 0){    				
    				foreach($friends as $friend){    					
    					if( $story->membership()->isMember($friend, null) ) continue;
                        $form->friend_id->addMultiOption($friend->getIdentity(), $friend->getTitle());
                        if(!$default_value) $default_value = $friend->getIdentity();
    				}
                    if($default_value) $form->friend_id->setValue($default_value);
    			} else {
    				$form->removeElement('submit');
    			}
    			
    			
    		} else {
    			$this->view->form = new Story_Form_Create_Custom(array(
    					'category' => Story_Model_Category::STORY_THREAD,
    					'tit' => 'Story Thread',
    			));
    			
    		}
    		$this->view->type = $type;
    		$chapter_number = $story->getTotalChapters();
            $this->view->lastChapter = $story->getLastChapter();
    		$this->view->story_id = $story_id;
    		$this->view->story = $story;
    		$this->view->chapter_id = $chapter_id;    
    		$this->view->prevChapter = $prevChapter;
            $this->view->is_owner = $story->owner_id==$viewer->getIdentity()?true:false;
    	}
        
        $this->view->chapter_number = $chapter_number;
    }

}
